package protocol.transactions;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.netease.http.THttpRequest;


import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.UserDynamic;

public class GetDynamicIdsTransaction extends XoneBaseTransaction {

    private int mAllcount;
    private int mReturnCount;

    public GetDynamicIdsTransaction(int allCount, int returnCount) {
        super(TRANSACTION_TYPE_GET_DYNAMIC_IDS);
        mAllcount = allCount;
        mReturnCount = returnCount;
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createGetDynamicIdsRequest(mAllcount, mReturnCount);
        sendRequest(request);
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj != null && obj instanceof JsonElement) {
            JsonElement dynamicListJson = ((JsonElement)obj).getAsJsonObject().get("dynamicList");
            JsonElement dynamicIdsJson = ((JsonElement)obj).getAsJsonObject().get("dynamicIds");
            List<UserDynamic> dynamicList;
            String[] ids;
            Gson gson = new Gson();
            Type collectionType = new TypeToken<List<UserDynamic>>(){}.getType();
            dynamicList = gson.fromJson(dynamicListJson, collectionType);
            ids = gson.fromJson(dynamicIdsJson, String[].class);
            Object[] data = new Object[2];
            data[0] = dynamicList;
            data[1] = ids;
            notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, data);
        } else {
            // TODO
            // notifyError(errCode, arg3)
        }
    }

}
